#include "audio.h"
#include "audio_sample.h"
#include <cstdlib>

using namespace krryn::audio;

sample::sample(int a_Frequency, int a_Channels) 
: m_Data(0), m_Count(0), m_Volume(1), m_Pan(0), m_SampleRate(a_Frequency), m_Channels(a_Channels) {}

sample::~sample(){
	if(m_Data)
		free(m_Data), m_Data = 0;
}

void sample::allocate(int a_BlockSize){
	// realloc is like malloc when m_Data is 0
	m_Data = (value_type*)realloc(m_Data, sizeof(value_type) * a_BlockSize);
	m_Count = a_BlockSize;
}

sample::value_type *sample::buffer() const{
	return m_Data;
}

int sample::channels() const{
	return m_Channels;
}

int sample::count() const{
	return m_Count;
}

int sample::sample_rate() const{
	return m_SampleRate;
}

float sample::pan() const{
	return m_Pan;
}

float sample::volume() const{
	return m_Volume;
}

void sample::set_count(int a_Count){
	if(a_Count <= m_Count){
		m_Count = a_Count;
	}else{
		throw audio::exception("Cannot increase count without calling allocate()");
	}
}

void sample::set_pan(float a_Pan){
	m_Pan = a_Pan;
}

void sample::set_volume(float a_Volume){
	m_Volume = a_Volume;
}
